<script setup>
import { ref, onMounted, watch } from 'vue';
import { useRoute } from 'vue-router';
import { ElMessage } from 'element-plus';
import http from "@/utils/http.js";

const route = useRoute();
const goodsList = ref([]);
const currentPage = ref(1);
const pageSize = ref(12);

// 获取搜索结果
const fetchSearchResults = async () => {
  const keyword = route.query.keyword;
  const pageNum = route.query.pageNum || 1;
  const pageSize = route.query.pageSize || 12;

  try {
    const res = await http.get('/goods/searchGoods/page', {
      params: {
        keyword,
        pageNum,
        pageSize
      }
    });
    if (res.data) {
      goodsList.value = res.data.records;
    }
  } catch (error) {
    ElMessage.error('搜索失败，请稍后重试');
    console.error(error);
  }
};

// 监听路由参数变化
watch(
  () => route.query,
  () => {
    fetchSearchResults();
  },
  { immediate: true }
);
</script> 